package ${ftl.packageName}.${cgH.moduleName};
<#assign en =0 />
<#assign cf =4 />
<#assign dao =1 />
import ${mList[en].packageName}.${cgH.moduleName}.${tc.entityName}${mList[en].classPostfix};
import ${mList[cf].packageName}.${cgH.moduleName}.${tc.entityName}${mList[cf].classPostfix};
import ${mList[dao].packageName}.${cgH.moduleName}.${tc.entityName}${mList[dao].classPostfix};
import org.springframework.stereotype.Service;
<#if "${cgH.isTree =='1'}">
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.stars.common.ObjectCopyUtils;
import com.stars.common.base.BaseConstant;
import com.stars.common.base.ResultMsg;
import com.stars.common.tree.TreeJson;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.stars.services.impl.BaseConfigTreeServiceImpl;
<#else>
import com.stars.services.impl.BaseConfigServiceImpl;
</#if>
/**
 * @Title: ${tc.tableComment} 业务逻辑
 * @author dj
 * @date ${tc.createTime}
 * @version V1.0
 *
 */
@Service
public class ${tc.entityName}${tc.entityPostfix}  extends <#if "${cgH.isTree =='1'}">BaseConfigTreeServiceImpl<#else >BaseConfigServiceImpl</#if><${tc.entityName}${mList[dao].classPostfix} , ${tc.entityName}${mList[en].classPostfix}> implements ${tc.entityName}${mList[cf].classPostfix} {

<#if "${cgH.isTree =='1'}">
    @Autowired
    private ${tc.entityName}${mList[dao].classPostfix} ${tc.entityName ?uncap_first}${mList[dao].classPostfix};

    /**
    * 添加菜单数据
    * @param treeJson
    * @param map
    * @throws Exception
    */
    public void add(TreeJson treeJson, Map<String,String> map) throws Exception{
        Gson gson = new Gson();
        if(StringUtils.isBlank(treeJson.getAddJson())){
           return;
        }
        List<${tc.entityName}${mList[en].classPostfix}> addList= gson.fromJson(treeJson.getAddJson(), new TypeToken<List<${tc.entityName}${mList[en].classPostfix}>>() {}.getType());
        for (${tc.entityName}${mList[en].classPostfix} add : addList) {
           //不包含Ext情况不往下走
           if(add.getPid().indexOf("Ext") ==-1){
             ${tc.entityName}${mList[en].classPostfix} entity = toAddEntity(treeJson, add);
             insert(entity);
             map.put(add.getId(),entity.getId());
             addParentId(add,entity.getId(),addList,treeJson,map);
           }
        }
    }
      /**
      * 递归添加树形
      * @param addList
      * @param treeJson
      */
      private void addParentId(${tc.entityName}${mList[en].classPostfix} root,String parentId,List<${tc.entityName}${mList[en].classPostfix}> addList,TreeJson treeJson, Map<String,String> map) throws Exception{
          for(${tc.entityName}${mList[en].classPostfix} c:addList){
             if(root.getId().equals(c.getPid())){
               ${tc.entityName}${mList[en].classPostfix} cc = toAddEntity(treeJson, c);
               cc.setPid(parentId);
               insert(cc);
               map.put(c.getId(),cc.getId());
               addParentId(c,cc.getId(),addList,treeJson,map);
             }
          }
          }
      private ${tc.entityName}${mList[en].classPostfix} toAddEntity(TreeJson treeJson, ${tc.entityName}${mList[en].classPostfix} add) {
          ${tc.entityName}${mList[en].classPostfix} entity = new ${tc.entityName}${mList[en].classPostfix}();
           ObjectCopyUtils.copyPropertiesIf(add,entity, BaseConstant.ADD);
            ObjectCopyUtils.copyPropertiesIf(treeJson,entity, BaseConstant.ADD);
           return entity;
       }
       /**
       * 更新菜单数据
       * @param treeJson
       * @param map
       * @throws Exception
       */
       public void update(TreeJson treeJson,Map<String,String> map) throws Exception{
           Gson gson = new Gson();
           if(StringUtils.isBlank(treeJson.getUpdateJson())){
               return;
           }
           List<${tc.entityName}${mList[en].classPostfix}> updateList= gson.fromJson(treeJson.getUpdateJson(), new TypeToken<List<${tc.entityName}${mList[en].classPostfix}>>() {}.getType());
             for (${tc.entityName}${mList[en].classPostfix}  updateTree : updateList) {
                 ${tc.entityName}${mList[en].classPostfix}  entity = ${tc.entityName ?uncap_first}${mList[dao].classPostfix}.selectByPrimaryKey(updateTree.getId());
                 if(null == entity){
                     return;
                 }
                 ObjectCopyUtils.copyPropertiesIf(updateTree,entity,BaseConstant.UPDATE);
                 entity.setUpdateInfo(treeJson);
                 //如果包含ext ，不往下走
                 if(StringUtils.isNotBlank(updateTree.getPid()) && updateTree.getPid().indexOf("Ext") ==0){
                    //如果包含
                    entity.setPid(map.get(updateTree.getPid()));
                 }
                 updateSelectiveById(entity);
             }
         }

</#if>

}